﻿using CodeGenerate.Model;
using ShareCode.Util.Helper;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace CodeGenerate
{
    /// <summary>
    /// WindowA.xaml 的交互逻辑
    /// </summary>
    public partial class WindowA : Window
    {
        string connstr;
        ViewModel viewModel;
        public WindowA()
        {
            InitializeComponent();
            Init();
        }


        private void Init()
        {
            connstr = "server=127.0.0.1;database=estimate;uid=root;pwd=123456;charset=utf8;Allow Zero Datetime=True;Convert Zero Datetime=True";
            viewModel = new ViewModel(connstr)
            {
                ShareEntityPath = "D:\\dotnet\\work\\appraise\\appraise",
                ServerEntityPath = "D:\\dotnet\\work\\appraise\\appraise",
                ServerDaoPath = "D:\\dotnet\\work\\appraise\\appraise",
                ServerBllPath = "D:\\dotnet\\work\\appraise\\appraise",
                ServerControllerPath = "D:\\dotnet\\work\\appraise\\appraise",
                ClientDaoPath = "D:\\dotnet\\work\\appraise\\appraise",

                ShareEntityNamespace = "ShareCode\\Entity",
                ServerEntityNamespace = "Infrastructure\\Entity",
                ServerDaoNamespace = "Dao",
                ServerBllNamespace = "Bll",
                ServerControllerNamespace = "ApiApp\\Controllers",
                ClientDaoNamespace = "ClientDao"
            };

            var mydb = viewModel.GetDb();

            var tables = mydb.DbMaintenance.GetTableInfoList(false);

            Dictionary<string, object> mappings = new Dictionary<string, object>();
            foreach (var table in tables)
            {
                mappings.Add(table.Name, table);
            }
            this.cbbTables.ItemsSource = mappings;


            this.cbbPClass.ItemsSource = EnumHelper.GetAllItems<PEnum>();
            this.cbbPClass.SelectedValue = "Value";
            this.cbbPClass.DisplayMemberPath = "Name";

        }


        //重新生成
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            if (String.IsNullOrEmpty(this.cbbPClass.Text) || cbbTables.SelectedItems.Count < 1) return;


            viewModel.SonPath = this.txtSonPath.Text;
            viewModel.PClass = (PEnum)Enum.Parse(typeof(PEnum), this.cbbPClass.Text);

            viewModel.RewriteShareEntity = true;
            viewModel.RewriteServerEntity = true;
            viewModel.RewriteServerDao = true;
            viewModel.RewriteServerBll = true;
            viewModel.RewriteServerController = true;
            viewModel.RewriteClientDao = true;

            foreach (var item in cbbTables.SelectedItems)
            {
                var aa = (DbTableInfo)item.Value;
                if (String.IsNullOrEmpty(viewModel.SonPath))
                {
                    var strItemTarget = aa.Name.Split('_')[0];
                    strItemTarget = System.Threading.Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase(strItemTarget);
                    viewModel.SonPath = strItemTarget;
                }
                viewModel.Gen(aa.Name);
            }


        }

        //修改生成
        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            if (String.IsNullOrEmpty(this.cbbPClass.Text) || cbbTables.SelectedItems.Count < 1) return;

            viewModel.SonPath = this.txtSonPath.Text;
            viewModel.PClass = (PEnum)Enum.Parse(typeof(PEnum), this.cbbPClass.Text);

            viewModel.RewriteShareEntity = cbShareEntity.IsChecked.Value;
            viewModel.RewriteServerEntity = cbServerEntity.IsChecked.Value;
            viewModel.RewriteServerDao = cbServerDao.IsChecked.Value;
            viewModel.RewriteServerBll = cbServerBll.IsChecked.Value;
            viewModel.RewriteServerController = cbServerController.IsChecked.Value;
            viewModel.RewriteClientDao = cbClientDao.IsChecked.Value;

            foreach (var item in cbbTables.SelectedItems)
            {
                var aa = (DbTableInfo)item.Value;

                if (String.IsNullOrEmpty(viewModel.SonPath))
                {
                    var strItemTarget = aa.Name.Split('_')[0];
                    strItemTarget = System.Threading.Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase(strItemTarget);
                    viewModel.SonPath = strItemTarget;
                }

                viewModel.Gen(aa.Name);
            }
        }
    }
}
